package com.yzc.android.network.request;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.yzc.android.network.base.KDRequestQueue;
import com.yzc.android.network.base.KDJsonObjectRequest;
import com.yzc.android.network.base.VolleyManager;
import org.json.JSONObject;

/**
 * Created by yzc on 16/4/7.
 */
public class PushRequestFactory {

    public static KDJsonObjectRequest uploadDeviceToken(final String token, final Response.Listener<JSONObject> successListener,
                                                        final Response.ErrorListener errorListener)
    {
        KDRequestQueue queue = VolleyManager.getRequestQueue();
        if (TextUtils.isEmpty(token))
        {
            // 请求参数错误的处理方式，便于统一处理界面逻辑
            queue.add(null, errorListener);
            return null;
        }

        Map<String, String> params = new HashMap<>();
        params.put("method", "c.my.infoview");
        KDJsonObjectRequest request = new KDJsonObjectRequest(Request.Method.POST, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (successListener != null)
                {
                    successListener.onResponse(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (errorListener != null)
                {
                    errorListener.onErrorResponse(error);
                }
            }
        });
        queue.add(request, errorListener);
        return request;
    }

    public static KDJsonObjectRequest unbindDeviceToken(final String token, final Response.Listener<JSONObject> successListener,
                                                        final Response.ErrorListener errorListener)
    {
        KDRequestQueue queue = VolleyManager.getRequestQueue();
        if (TextUtils.isEmpty(token))
        {
            // 请求参数错误的处理方式，便于统一处理界面逻辑
            queue.add(null, errorListener);
            return null;
        }

        Map<String, String> params = new HashMap<>();
        params.put("method", "c.my.infoview");
        KDJsonObjectRequest request = new KDJsonObjectRequest(Request.Method.POST, params, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (successListener != null)
                {
                    successListener.onResponse(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (errorListener != null)
                {
                    errorListener.onErrorResponse(error);
                }
            }
        });
        queue.add(request, errorListener);
        return request;
    }
}
